package utils;

import model.DoubleLinkNode;

/**
 * 双向链表
 */
public class DoubleLinkUtil extends AbstractLineClass {
    public DoubleLinkUtil() {
        super(0);
    }

    public DoubleLinkNode linkInitiate(int size) {
        DoubleLinkNode head = null;
        DoubleLinkNode preNode = null;
        for (int i = 0; i < size; i++) {
            if (i > 0) {
                final DoubleLinkNode newNode = new DoubleLinkNode();
                newNode.setData(i);
                newNode.setBeforeNode(preNode);
                preNode.setAfterNode(newNode);
                preNode = newNode;
            } else {
                head = new DoubleLinkNode();
                preNode = head;
            }
        }
        super.setSize(size);
        return head;
    }

    public void printLink(DoubleLinkNode head) {
        if (head != null) {
            int index = 0;
            do {
                System.out.println(String.format("序号：%d，值：%d", index++, head.getData()));
                head = head.getAfterNode();
            } while (head != null);
        }
    }

    public DoubleLinkNode insertDate(DoubleLinkNode head, int site, int value) {
        if (!checkSite(site))
            return null;
        DoubleLinkNode currentNode = head;
        // TODO: 2023/11/7 将当前位置停止在预计删除的序号的元素
        int index = 0;
        while (currentNode != null && index < site) {
            currentNode = currentNode.getAfterNode();
            index++;
        }
        if (index != site) {
            return null;
        }
        final DoubleLinkNode newNode = new DoubleLinkNode();
        newNode.setData(value);
        newNode.setAfterNode(currentNode.getAfterNode());
        newNode.setBeforeNode(currentNode);
        currentNode.setAfterNode(newNode);
        super.addSize();
        return head;
    }

    public DoubleLinkNode deleteData(DoubleLinkNode head, int site) {
        if (!checkSite(site))
            return null;
        DoubleLinkNode currentNode = head;
        // TODO: 2023/11/7 将当前位置停止在预计删除的元素
        // TODO: 2023/11/7 思考一下，停止的位置选项包括：前驱元素、当前预删除元素、后续元素，停止在哪里最安全，为什么？
        int index = 0;
        while (currentNode != null && index < site) {
            currentNode = currentNode.getAfterNode();
            index++;
        }
        if (index != site) {
            return null;
        }
        // TODO: 2023/11/7 得到预删除元素的前驱元素
        final DoubleLinkNode beforeNode = currentNode.getBeforeNode();
        // TODO: 2023/11/7 得到预删除元素的后续元素
        final DoubleLinkNode afterNode = currentNode.getAfterNode();
        if (beforeNode != null) {
            beforeNode.setAfterNode(afterNode);
        }
        if (afterNode != null) {
            afterNode.setBeforeNode(beforeNode);
        }
        super.subtractionSize();
        return head;
    }

    public DoubleLinkNode updateData(DoubleLinkNode head, int site, int updateValue) {
        if (!checkSite(site))
            return null;
        DoubleLinkNode currentNode = head;
        // TODO: 2023/11/7 将当前位置停止在预计删除的序号的元素
        int index = 0;
        while (currentNode != null && index < site) {
            currentNode = currentNode.getAfterNode();
            index++;
        }
        if (index != site) {
            return null;
        }
        currentNode.setData(updateValue);
        return head;
    }

    public boolean destroyLink(DoubleLinkNode head) {
        // TODO: 2023/11/7 JAVA中的对象内存，它会自动释放，无需free
        // TODO: 2023/11/7 这里我们以将值改为-1，标识销毁
        boolean resultValue = false;
        if (head != null) {
            do {
                super.subtractionSize();
                head.setData(-1);
                head = head.getAfterNode();
            } while (head != null);
            resultValue = true;
        }
        return resultValue;
    }
}
